package com.lc.qianrenshiye.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/30.
 */
@HttpInlet(Conn.ORDER_DETAILS)
public class OrderDetailGet extends BaseAsyGet<OrderDetailGet.OrderDetailEntity> {

    public String oid;

    public OrderDetailGet(AsyCallBack<OrderDetailEntity> asyCallBack) {
        super(asyCallBack);
    }

    @Override
    protected OrderDetailEntity parser(JSONObject object) throws Exception {

        TOAST = object.optString("message");

        if (object.optInt("code") == 200) {

            OrderDetailEntity orderDetailEntity = new OrderDetailEntity();

            JSONObject mianObject = object.optJSONObject("data");

            orderDetailEntity.id = mianObject.optString("id");
            orderDetailEntity.paystatus = mianObject.optString("paystatus");

            JSONArray goodsArray = mianObject.optJSONArray("goods");

            if (goodsArray != null) {

                for (int i = 0; i < goodsArray.length(); i++) {

                    JSONObject goodsObject = goodsArray.optJSONObject(i);

                    Goods goods = new Goods();

                    goods.gid = goodsObject.optString("gid");
                    goods.title = goodsObject.optString("title");
                    goods.deprice = goodsObject.optString("deprice");
                    goods.picurl = Conn.IMAGE_URL + goodsObject.optString("picurl");
                    goods.num = goodsObject.optString("num");

                    orderDetailEntity.goodsList.add(goods);

                }

            }

            JSONArray addgoodsArray = mianObject.optJSONArray("addgoods");

            if (goodsArray != null) {

                for (int i = 0; i < addgoodsArray.length(); i++) {

                    JSONObject addObject = addgoodsArray.optJSONObject(i);

                    Addeds addeds = new Addeds();

                    addeds.gid = addObject.optString("gid");
                    addeds.title = addObject.optString("title");
                    addeds.deprice = addObject.optString("deprice");
                    addeds.picurl = Conn.IMAGE_URL + addObject.optString("picurl");
                    addeds.num = addObject.optString("num");

                    orderDetailEntity.addedsList.add(addeds);

                }

            }

            return orderDetailEntity;

        }

        return null;
    }

    public static class OrderDetailEntity {

        public String id, paystatus;

        public List<Goods> goodsList = new ArrayList<>();
        public List<Addeds> addedsList = new ArrayList<>();

    }

    public static class Goods {

        public String gid, title, deprice, picurl, num;

    }

    public static class Addeds {

//        "gid": "1",
//                "title": "水煮鱼",
//                "deprice": "30.00",
//                "picurl": "/Uploads/20160913/pic_57d76b63e03ef.jpg",
//                "num": "1"
        public String gid, title, deprice, picurl, num;

    }

}
